﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.Text;
using IFPRJ.EF.Model;
using IFPRJ.EF.DALayer;

namespace IFPRJ.EF.DALayer.Providers.Sqlserver
{
    public class NewsProvider:INews
    {
        public static IFDBContext dbcontext = new IFDBContext();

        public NewsProvider()
        { 
        }

        IFDBContext GetCurrentDBContext()
        {
            if (dbcontext == null)
                return new IFDBContext();
            else
                return dbcontext;
            
        }


        public bool NewsCreate(News news)
        {
            IFDBContext db=GetCurrentDBContext();
            bool result = false;
            try
            {
                db.Newses.Add(news);
                db.SaveChanges();
                result = true;
            }
            catch (Exception e)
            {
                throw e;
         
            }
            return result;
        }

        public bool NewsUpdate(News news)
        {

            IFDBContext db = GetCurrentDBContext();
            bool result = false;
            try
            {
                db.Entry<News>(news).State = EntityState.Modified;
                db.SaveChanges(); 
                result = true;
               
                
            }
            catch (Exception e)
            {
                throw e;

            }
            return result;
        }

        public bool NewsDelete(News news)
        {
            IFDBContext db = GetCurrentDBContext();
            bool result = false;
            try
            {
                db.Newses.Remove(news);
                db.SaveChanges();
                result = true;
            }
            catch (Exception e)
            {
                throw e;

            }
            return result;
             
        }

        public List<News> NewsLoadAll()
        {
            IFDBContext db = GetCurrentDBContext();
            return  db.Newses.Take(10).ToList();
            
        }

        public News NewsGetSingleByNewsId(int newsid)
        {
            IFDBContext db = GetCurrentDBContext();
            News news =db.Newses.Single(item => item.ID == newsid);
            return news ?? null;
            
        }

        public List<News> NewsGetListByCatoryId(int catoryid)
        {
            IFDBContext db = GetCurrentDBContext();
            
            var newslist=   from p in db.Newses
                            where p.News_CatoryID == catoryid
                            select p;
            return newslist.ToList();

        }
    }



    public class NewsCatoryProvider : INewsCatory
    {
        public static IFDBContext dbcontext = new IFDBContext();

        public NewsCatoryProvider()
        { 
        }

        IFDBContext GetCurrentDBContext()
        {
            if (dbcontext == null)
                return new IFDBContext();
            else
                return dbcontext;
            
        }
        public bool NewsCatoryCreate(News_Catory news_Catory)
        {
            IFDBContext db = GetCurrentDBContext();
            bool result = false;
            try
            {
                db.News_Catories.Add(news_Catory);
                db.SaveChanges();
                result = true;
            }
            catch (Exception e)
            {
                throw e;

            }
            return result;
        }

        public bool NewsCatoryUpdate(News_Catory news_Catory)
        {
            IFDBContext db = GetCurrentDBContext();
            bool result = false;
            try
            {
                db.Entry<News_Catory>(news_Catory).State = EntityState.Modified;
                db.SaveChanges();
                result = true;


            }
            catch (Exception e)
            {
                throw e;

            }
            return result;
        }

        public bool NewsCatoryDelete(News_Catory news_Catory)
        {
            IFDBContext db = GetCurrentDBContext();
            bool result = false;
            try
            {
                db.News_Catories.Remove(news_Catory);
                db.SaveChanges();
                result = true;
            }
            catch (Exception e)
            {
                throw e;

            }
            return result;
        }

        public List<News_Catory> NewsCatoryLoadAll()
        {
            IFDBContext db = GetCurrentDBContext();
            return db.News_Catories.Take(10).ToList();
        }

        public News_Catory NewsCatoryGetSingleByCatoryId(int news_Catoryid)
        {
            IFDBContext db = GetCurrentDBContext();
            News_Catory newscatory = db.News_Catories.Single(item => item.ID == news_Catoryid);
            return newscatory ?? null;
        }
    }
}
